package com.frame.util;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class BeanUtils {

	public static final Class[] ecs = new Class[0];

	public static final Object[] eos = new Object[0];

	/**
	 * 示例：public Object get(String name) throws Exception
	 *{
	 * 		return BeanUtils.getValue(this,name);
	 *}
	 * @param obj
	 * @param property
	 * @return
	 * @throws Exception
	 */
	static public Object getValue(Object obj,String property) throws Exception
	{
		String methodName = Character.toUpperCase(property.charAt(0)) +
				property.substring(1);
		Method method = null;

		try
		{
			method = obj.getClass().getMethod("get" + methodName,ecs);
		}
		catch(NoSuchMethodException ex)
		{
			try
			{
				method = obj.getClass().getMethod("is" + methodName,ecs);
			}
			catch(NoSuchMethodException ex1)
			{
				throw ex;
			}
		}

		try
		{
			return method.invoke(obj,eos);
		}
		catch(InvocationTargetException ex)
		{
//	         handleInvocationTargetException(ex);
			return null;
		}
	}
	static public void setValue(Object obj,String property,Object value) throws
			Exception
	{
		String methodName = "set" + Character.toUpperCase(property.charAt(0)) +
				property.substring(1);
		Method method = null;

		Class c = obj.getClass();
		Object valueObj=null;
		try
		{
			if(value!=null)
				method = c.getMethod(methodName,new Class[]
						{value.getClass()});
			valueObj=value;
		}
		catch(NoSuchMethodException ex)
		{
			Class type=getType(c,property);
			method = c.getMethod(methodName,new Class[]
					{type});
			if(value!=null)
				valueObj=ConvertFactoy.convertValue(type, new String[]{value.toString()});
		}
		method.invoke(obj,new Object[]
				{valueObj});
	}
	static public void setValue(Object obj,String property,Object value,Class propertyType) throws
			Exception
	{
		String methodName = "set" + Character.toUpperCase(property.charAt(0)) +
				property.substring(1);
		Method method = null;

		Class c = obj.getClass();
		Object valueObj=null;
		try
		{

			method = c.getMethod(methodName,new Class[]
					{propertyType});
			if(value!=null)
				valueObj=ConvertFactoy.convertObject(propertyType, value);
		}
		catch(NoSuchMethodException ex)
		{
			Class type=getType(c,property);
			method = c.getMethod(methodName,new Class[]
					{type});
			if(value!=null)
				valueObj=ConvertFactoy.convertObject(type, value);
		}
		method.invoke(obj,new Object[]
				{valueObj});
	}
	/**
	 * 获得属性的类型
	 * @param c
	 * @param property
	 * @return
	 * @throws Exception
	 */
	static public Class getType(Class c,String property) throws Exception
	{
		String methodName = Character.toUpperCase(property.charAt(0)) +
				property.substring(1);
		Method method = null;
		try
		{
			method = c.getMethod("get" + methodName,ecs);
		}
		catch(NoSuchMethodException ex)
		{
			try
			{
				method = c.getMethod("is" + methodName,ecs);
			}
			catch(NoSuchMethodException ex1)
			{
				throw ex;
			}
		}

		return method.getReturnType();
	}
}
